<template>
  <div id="app">
    <header class="head">
      <a class="back tl" href="javascript:history.back(-1)">
        <i class="el-icon-arrow-left"></i>
      </a>
      <a href="javascript:;" class="tc">
        <small class="font_18 co_f">添加食品名称</small>
      </a>
      <a href="javascript:;" class="save">
        <button @click="submitForm()" type="primary">保存</button>
      </a>
    </header>
    <main>
      <div class="formcontent">
        <ul>
          <li>
            <span>食品类别</span>
            <div class="froms">
              <el-select v-model="formName.type" placeholder="请选择">
                <el-option v-for="item in selectData" :key="item.id" :label="item.name" :value="item.id">
                </el-option>
              </el-select>
            </div>
          </li>
          <li>
            <span>食品名称</span>
            <div class="froms">
              <input v-model="formName.name">
            </div>
          </li>
          <li>
            <span><em>*</em>品牌</span>
            <div class="froms">
              <input v-model="formName.brand" name="brand" v-validate="'required'" :class="{'is-danger': errors.has('brand')}">
            </div>
          </li>
          <li>
            <span><em>*</em>生产商</span>
            <div class="froms">
              <input v-model="formName.producers" name="producers" v-validate="'required'" :class="{'is-danger': errors.has('producers')}">
            </div>
          </li>
          <li>
            <span>净含量/规格</span>
            <div class="froms">
              <input v-model="formName.norms">
            </div>
          </li>
          <li>
            <span><em>*</em>描述</span>
            <div class="froms">
              <input v-model="formName.describe" name="describe" v-validate="'required'" :class="{'is-danger': errors.has('describe')}">
            </div>
          </li>
        </ul>
      </div>
  
    </main>
  </div>
</template>

<script>
  import {
    DemandWarehouseAdd,
    DemandTypeBind
  } from '../../../api/api'
  export default {
    name: 'DemandWarehouseAdd',
    data() {
      return {
        formName: {
          type: '',
          name: '',
          brand: '',
          producers: '',
          norms: '',
          describe: '',
        },
        selectData: {},
        value: ''
      };
    },
    methods: {
      //下拉选择
      typeselect() {
        let options = {
          params: {
            enterpriseUnid: JSON.parse(localStorage.getItem('auth')).enterpriseUnid,
          }
        }
        DemandTypeBind(options).then((res) => {
          if (res.success === true) {
            this.selectData = res.data;
          }
        })
  
      },
      //提交方法
      submitForm: function() {
        this.$validator.validateAll().then((result) => {
          if (result) {
            let varDemandType = {
              Unid: this.formName.type
            }
            let data = {
              DemandType: varDemandType,
              Name: this.formName.name,
              Brand: this.formName.brand,
              Producers: this.formName.producers,
              Norms: this.formName.norms,
              Describe: this.formName.describe,
            }
            let dataoption = {
              params: {
                Data: JSON.stringify(data),
                UserUnid: JSON.parse(localStorage.getItem('auth')).UserUnid,
              }
            }
            //调取接口
            DemandWarehouseAdd(dataoption).then((res) => {
              if (res.success === true) {
                this.$message.success('保存成功');
                this.$router.go(-1);
              } else {
                this.$message.error(res.msg);
              }
            }).catch(function() {
              this.$message.error('网络异常。');
            })
  
          } else {
              this.$message.error('带*号为必填项，请确认填写格式正确');
          }
        })
  
      },
  
    },
    created() {
      this.typeselect();
    },
  }
</script>

<style lang="scss">
  
</style>
